package com.mkyong.rest;

import java.util.ArrayList;
import java.util.List;

import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import com.hp.hpl.jena.query.QueryException;
import com.mkyong.app.Node;
import com.mkyong.app.Profiling;
import com.mkyong.app.QueryExecutor;
import com.mkyong.app.Response;

@Path("/message")
public class MessageRestService {

	@POST
	@Path("/patient")
	@Consumes({ MediaType.APPLICATION_JSON })
	@Produces({ MediaType.APPLICATION_JSON })
	public Response getPatient(Request requestObj) {

		QueryExecutor executor;
		Response response = new Response();
		List<Profiling> profiles = new ArrayList<Profiling>();

		
		if (requestObj.isCustomQuery()) {
			executor = new QueryExecutor(requestObj.getQuery());
		} else {
			executor = new QueryExecutor();
		}

		try {
			long start = System.currentTimeMillis();
			List<Node> map = executor.getFromRelationalStore();
			long end = System.currentTimeMillis();

			Profiling time = new Profiling("Relational Database", end - start);
			profiles.add(time);

			start = System.currentTimeMillis();
			executor.getFromOwlFileStore();
			end = System.currentTimeMillis();

			time = new Profiling("Owl File", end - start);
			profiles.add(time);

			start = System.currentTimeMillis();
			executor.getFromTripleStore();
			end = System.currentTimeMillis();

			time = new Profiling("Triple Store", end - start);
			profiles.add(time);
			
			response.setNodes(map);
		} catch (QueryException e) {
			response.setResponseCode("QUERY_ERROR");
		}
	

		response.setProfiles(profiles);
		
		return response;

	}

}